package cn.juni.controller;

import cn.juni.pojo.Permission;
import cn.juni.pojo.Role;
import cn.juni.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Juni
 * @date 2021/2/26
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @RequestMapping("/findAll")
    public String findAll(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Model model){
        List<Role> roleList = roleService.findAllRoles(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(roleList);
        model.addAttribute("pageInfo",pageInfo);
        return "role-list";
    }

    @RequestMapping("/save")
    public String save(Role role){
        roleService.insertRole(role);
        return "redirect:findAll";
    }

    @RequestMapping("/findRole")
    public String findRoleDesc(String id,Model model){
        Role role = roleService.findRole(id);
        model.addAttribute("role",role);
        return "role-show";
    }

    @RequestMapping("/findOtherPermission")
    public String findOtherPermission(String roleId,Model model){
        List<Permission> permissionList = roleService.findOtherPermission(roleId);
        model.addAttribute("permissionList",permissionList);
        model.addAttribute("roleId",roleId);
        return "role-permission-add";
    }

    @RequestMapping("/saveRolePermission")
    public String saveRolePermission(String roleId,String[] ids){
        roleService.inserRolePermission(roleId,ids);
        return "redirect:findAll";
    }
}
